
import { promisify } from '../../utils/promise.util'
import { $init, $digest } from '../../utils/common.util'

const app = getApp()
Page({
  data: {
    name: "",
    phone: "",
    conpanyName: "",
    hangyeindex: 0,
    hangye: [],
    projectindex:0,
    project: [],
    xuqiuindex: 0,
    xuqiu: [],
    xuqiu2index: 0,
    xuqiu2: [],
    mianjiindex: 0,
    mianji: [],
    mianjiAll: [],
    region:["广东省","广州市",""],
    qudaoindex: 0,
    qudao: [],
    content: "",
    images: [],
    upload: [],
    pingfangmi:"m²",
    showModal: false,
    tishi:false,
    companyShow: true,
    hangyeShow: true,
  },
  onLoad: function () {
    wx.setNavigationBarTitle({
      title: '新增客户',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })

    $init(this)

    //获取行业数据
    var that = this;


    //获取项目
    wx.request({
      url: app.globalData.httpurl +'v2/project',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {

      },
      success: function (res) {
        that.setData({
          project: res.data.data
        })
        wx.request({
          url: app.globalData.httpurl +'industry',
          method: "GET",
          header: {
            "Authorization": "bearer " + app.globalData.access_token
          },
          data: {
            project_id:that.data.project[0].project_id
          },
          success: function (res) {
            that.setData({
              hangye: res.data.data
            })
          }
        })
      }
    })
    

    //获取渠道
    wx.request({
      url: app.globalData.httpurl +'source',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {

      },
      success: function (res) {
        that.setData({
          qudao: res.data.data
        })
      }
    })

    //获取面积数据
    wx.request({
      url: app.globalData.httpurl + 'area',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {

      },
      success: function (res) {
        var datas=res.data.data;
        for (var i = 0; i < datas.length; i++) {
          datas[i]["is_display"] = datas[i]["is_display"] == null ? 0 : datas[i]["is_display"]
          datas[i]["name"] = datas[i]["name"]+that.data.pingfangmi
        }
        that.setData({
          mianji: datas,
          mianjiAll: datas,
        })
      }
    })

    //获取需求数据
    wx.request({
      url: app.globalData.httpurl + 'demand',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {

      },
      success: function (res) {
        var datas = res.data.data;
        that.setData({
          xuqiu: datas
        })
      }
    })

    //获取需求数据2
    wx.request({
      url: app.globalData.httpurl + 'demand',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        type: 1
      },
      success: function (res) {
        that.setData({
          xuqiu2: res.data
        })
      }
    })
  },
  chooseImage(e) {
    wx.chooseImage({
      count: 3,
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        const images = this.data.images.concat(res.tempFilePaths)
        // 限制最多只能留下3张照片
        this.data.images = images.length <= 1 ? images : images.slice(0, 3)
        $digest(this)

        this.uploadImg();
      }
    })
  },
  removeImage(e) {
    const idx = e.target.dataset.idx
    this.data.images.splice(idx, 1)
    $digest(this)
  },
  handleImagePreview(e) {
    const idx = e.target.dataset.idx
    const images = this.data.images
    wx.previewImage({
      current: images[idx],  //当前预览的图片
      urls: images,  //所有要预览的图片
    })
  },
  uploadImg: function () {
    var that = this;
    wx.uploadFile({
      url: app.globalData.httpurl +'upload',
      filePath: this.data.images[0],
      name: 'images',
      success: function (res) {
        var result=JSON.parse(res.data);
        if (result.status == 1) {
          that.data.upload.push(result.data);
        }
      }
    })
  },
  bindname:function(e){
    this.setData({
      name: e.detail.value
    })
  },
  bindphone: function (e) {
    var inputPhone = e.detail.value
    this.setData({
      tishi: false
    })
    if (inputPhone.length == 11){
      //检查号码可用性

      //如果号码不可用进行提示
      // this.setData({
      //   tishi:true
      // })
      this.setData({
        phone: inputPhone
      })
    }
  },
  bindconpanyName: function (e) {
    this.setData({
      conpanyName: e.detail.value
    })
  },
  bindhangye: function (e) {
    this.setData({
      hangyeindex: e.detail.value
    })
  },
  bindproject: function (e) {
    this.setData({
      projectindex: e.detail.value
    })

    var that=this
    wx.request({
      url: app.globalData.httpurl +'industry',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        project_id:that.data.project[that.data.projectindex].project_id
      },
      success: function (res) {
        that.setData({
          hangye: res.data.data
        })
      }
    })

    if(this.data.project[this.data.projectindex].name == '招商采盘'){

      this.setData({
        mianji: [],
        companyShow: false
      })
      for(var i = 0 ;i < this.data.mianjiAll.length;i++){
        
        var item = "mianjiAll[" + i + "].name";

        this.setData({
          [item]: this.data.mianjiAll[i].name.replace("m²", "亩")
        })
        if (this.data.mianjiAll[i].is_display == 1) {
          var length = this.data.mianji.length
          this.data.mianji[length] = this.data.mianjiAll[i]
          this.setData({
            mianji: this.data.mianji
          })
        }
      }
    }else{
      this.setData({
        mianji: [],
        companyShow: true
      })
      for(var i = 0 ;i < this.data.mianjiAll.length;i++){
        var item="mianjiAll["+i+"].name";
        this.setData({
          [item]: this.data.mianjiAll[i].name.replace("亩","m²")
        })

        var length = this.data.mianji.length
        this.data.mianji[length] = this.data.mianjiAll[i]
        this.setData({
          mianji: this.data.mianji
        })
      }
    }
  },
  bindxuqiu: function (e) {
    this.setData({
      xuqiuindex: e.detail.value
    })
  },
  bindxuqiu2: function (e) {
    this.setData({
      xuqiu2index: e.detail.value
    })
  },
  bindmianji: function (e) {
    this.setData({
      mianjiindex: e.detail.value
    })
  },
  bindregion: function(e) {
    this.setData({
      region: e.detail.value
    })
  },
  bindqudao: function(e){
    this.setData({
      qudaoindex: e.detail.value
    })
  },
  bindcontent: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  subInfo:function(e){
    if(this.data.name == ''){
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    if (this.data.phone == ''){
      wx.showToast({
        title: '联系方式不能为空',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    // if (this.data.conpanyName == '') {
    //   wx.showToast({
    //     title: '公司名称不能为空',
    //     icon: 'none',
    //     duration: 1500
    //   })
    //   return;
    // }
    if (this.data.content == '') {
      wx.showToast({
        title: '跟进情况不能为空',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    // if (this.data.upload.length == 0) {
    //   wx.showToast({
    //     title: '请选择图片上传',
    //     icon: 'none',
    //     duration: 1500
    //   })
    //   return;
    // }
    var that = this
    //提交数据
    wx.request({
      url: app.globalData.httpurl +'coustomer/create',
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      method:"POST",
      data: {
        "name":this.data.name,
        "mobile": this.data.phone,
        "company_name": this.data.conpanyName,
        "industry_id": this.data.companyShow ? this.data.hangye[this.data.hangyeindex].industry_id: "",//行业
        "project_id": this.data.project[this.data.projectindex].project_id,//项目
        "source_id": this.data.qudao[this.data.qudaoindex].source_id,//渠道
        "demand_id": this.data.companyShow ? this.data.xuqiu[this.data.xuqiuindex].id:this.data.xuqiu2[this.data.xuqiu2index].id,//需求类型
        "area_id": this.data.mianji[this.data.mianjiindex].id,//需求面积
        "city1": this.data.region[0],
        "city2": this.data.region[1],
        // address: this.data.address,//地址
        "desc": this.data.content,//备注
        "image": this.data.upload.length == 0?"":this.data.upload[0],//图片
      },
      success: function (res) {
        if(res.data.status == 1){
          that.setData({
            showModal: true
          })
        }else{
          if( res.data.message == 'unique'){
            that.setData({
              tishi: true
            })
            that.goTop()
          }else{
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      }
    })
  },
  goTop: function (e) {  // 一键回到顶部
    if (wx.pageScrollTo) {//判断这个方法是否可用
       wx.pageScrollTo({
         scrollTop: 0
       })
     } else {
       wx.showModal({
         title: '提示',
         content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
       })
     }
   },
  /**
    * 弹出框蒙层截断touchmove事件
    */
  preventTouchMove: function () {
  },
  /**
    * 对话框继续添加点击事件
    */
  onCancel: function () {
    if (app.globalData.isCustomerDistribution) {
      wx.navigateTo({
        url: '/pages/fenpeilist/fenpeilist',
      })
    } else {
      wx.navigateTo({
        url: '/pages/list/listCustom',
      })
    }
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.setData({
      name: "",
      phone: "",
      conpanyName: "",
      hangyeindex: 0,
      projectindex:0,
      xuqiuindex: 0,
      mianjiindex: 0,
      region:["广东省","广州市",""],
      qudaoindex: 0,
      content: "",
      showModal: false,
    })
  }
})
